
import store from '@/store'
import Cookies from 'js-cookie'
import Vue from 'vue'
/**
 *
 * @returns Store data in user info
 */

export function getUserInfoCommon () {
  const params = {}
  if (sessionStorage.getItem('loginAsUserId')) {
    params.loginAsUserId = sessionStorage.getItem('loginAsUserId')
  }
  if (sessionStorage.getItem('loginAsRoleId')) {
    params.loginAsRoleId = sessionStorage.getItem('loginAsRoleId')
  }

  Vue.prototype.$interface.system.getUserInfoApi(params).then(({ data: res }) => {
    store.state.user.id = res.data.id
    store.state.user.name = (res.data.firstName == null ? '' : res.data.firstName) + ' ' + (res.data.lastName == null ? '' : res.data.lastName)
    store.state.user.superAdmin = res.data.superAdmin
    store.state.user.superTenant = res.data.superTenant
    store.state.user.tenantName = res.data.tenantName
    store.state.user.tenantCode = Cookies.get('tenant_code') ? Cookies.get('tenant_code') : res.data.tenantCode
    store.state.user.email = res.data.email
    store.state.user.firstName = res.data.firstName
    store.state.user.headUrl = res.data.headUrl
    store.state.user.lastName = res.data.lastName
    store.state.user.timezoneId = res.data.timezoneId
    store.state.user.timezoneName = res.data.timezoneName
    store.state.user.timezoneOffset = res.data.timezoneOffset
    store.state.user.username = res.data.username
    store.state.user.realName = res.data.realName
    store.state.user.tenantSwitch = res.data.tenantSwitch
    store.state.user.roleList = res.data.roleList
    store.state.user.deptId = res.data.deptId
    store.state.user.cwUser = res.data.cwUser
    // store.state.user.autotaskUrl = res.data.autotaskUrl
    // store.state.user.syncroMspUrl = res.data.syncroMspUrl
    // store.state.user.haloUrl = res.data.haloUrl
    // store.state.user.cwUrl = res.data.cwUrl
    store.state.user.domainUrl = res.data.domainUrl
    store.state.user.timeFormat = res.data.timeFormat
    store.state.user.tenantTimezoneId = res.data.tenantTimezoneId
    store.state.user.tenantTimezoneName = res.data.tenantTimezoneName
    store.state.user.tenantTimezoneOffset = res.data.tenantTimezoneOffset
    store.state.user.regSource = res.data.regSource
    store.state.user.tenantList = res.data.tenantList
  }).catch(() => { })
}

/**
 *
 * @returns dict data in all
 */

export function getDictInfoCommon () {
  return Vue.prototype.$interface.system.generalListApi().then(({ data: res }) => {
    window.SITE_CONFIG['dictList'] = res.data
  }).catch(() => { })
}
/**
 *
 * @returns permission data in all
 */
export function getPermissionsCommon () {
  const params = {}
  if (sessionStorage.getItem('loginAsUserId')) {
    params.loginAsUserId = sessionStorage.getItem('loginAsUserId')
  }
  if (sessionStorage.getItem('loginAsRoleId')) {
    params.loginAsRoleId = sessionStorage.getItem('loginAsRoleId')
  }
  return Vue.prototype.$interface.system.getUserPermissionsApi(params).then(({ data: res }) => {
    window.SITE_CONFIG['permissions'] = res.data
  }).catch(() => { })
}
